﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;
using CommunitySystem.PeiXunService;
using EHR.Common;

namespace CommunitySystem
{
    public partial class PeiXunWindow : ChildWindow
    {
        private PeiXunService.PeiXunServiceClient client1 = new PeiXunService.PeiXunServiceClient();
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private PeiXunService.PeiXunDetailModel model = new PeiXunService.PeiXunDetailModel();
        private int oldpiaoshu = 0;
        private int olddingpiaoshu = 0;
        public bool IsNew = false;
        private int peixunid = 0;
        private int OrgID;
        public PeiXunWindow(int id, bool isnew, string orgname, int orgid)
        {
            InitializeComponent();
            org.Text = orgname;
            OrgID = orgid;
            IsNew = isnew;
            peixunid = id;
            client1.GetPeiXunDetailModelCompleted += new EventHandler<PeiXunService.GetPeiXunDetailModelCompletedEventArgs>(client1_GetPeiXunDetailModelCompleted);
            //client1.GetPeiXunDetailModel1Completed += new EventHandler<PeiXunService.GetPeiXunDetailModel1CompletedEventArgs>(client1_GetPeiXunDetailModel1Completed);
            //client1.GetPeiXunDetailModel1Async(orgid.ToString(), id);
            if (isnew)
            {
                client1.GetPeiXunDetailModelAsync("0", 0, id);
            }
            else
            {
                client1.GetPeiXunDetailModelAsync("0", id, 0);
            }
            //if (juese == "人事干部")
            //{
            //    org.Visibility = System.Windows.Visibility.Visible;
            //    org1.Visibility = System.Windows.Visibility.Collapsed;
            //}
            //else
            //{
            //    org1.Visibility = System.Windows.Visibility.Visible;
            //    org.Visibility = System.Windows.Visibility.Collapsed;
            //}
            Client.GetOrgForHRModelbyIDsCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs>(Client_GetOrgForHRModelbyIDsCompleted);
            Client.GetDengLuOrgCompleted += new EventHandler<GetDengLuOrgCompletedEventArgs>(Client_GetDengLuOrgCompleted);
            Client.GetDengLuOrgAsync(Session.LoginUser.LoginName);
            client1.AddPeiXunDetailModelCompleted += new EventHandler<PeiXunService.AddPeiXunDetailModelCompletedEventArgs>(client1_AddPeiXunDetailModelCompleted);
            client1.UpdatePeiXunDetailModelCompleted += new EventHandler<PeiXunService.UpdatePeiXunDetailModelCompletedEventArgs>(client1_UpdatePeiXunDetailModelCompleted);
            client1.CanDingPiaoCompleted += new EventHandler<PeiXunService.CanDingPiaoCompletedEventArgs>(client1_CanDingPiaoCompleted);
            client1.CanDingPiao1Completed += new EventHandler<PeiXunService.CanDingPiao1CompletedEventArgs>(client1_CanDingPiao1Completed);
        }

        private int yijingpiaoshu = 0;
        void client1_GetPeiXunDetailModel1Completed(object sender, PeiXunService.GetPeiXunDetailModel1CompletedEventArgs e)
        {
            if (e.Result == 0)
            {
                yudingzhangshu1.Visibility = System.Windows.Visibility.Collapsed;
                yudingzhangshu.Visibility = System.Windows.Visibility.Collapsed;
                yudingzhangshu2.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                yudingzhangshu.Visibility = System.Windows.Visibility.Visible;
                yudingzhangshu1.Visibility = System.Windows.Visibility.Visible;
                yudingzhangshu2.Visibility = System.Windows.Visibility.Visible;
                yijingpiaoshu = e.Result;
                yudingzhangshu.Text = e.Result.ToString();
            }
        }


        void client1_UpdatePeiXunDetailModelCompleted(object sender, PeiXunService.UpdatePeiXunDetailModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("订票成功！");
                //result();
                this.DialogResult = true;
            }
        }


        public String[] Oldorgids;
        void Client_GetDengLuOrgCompleted(object sender, EmpbasicService.GetDengLuOrgCompletedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Result))
            {
                Oldorgids = e.Result.Split(',');
            }
            Client.GetOrgForHRModelbyIDsAsync("(" + e.Result + ")");
        }

        void Client_GetOrgForHRModelbyIDsCompleted(object sender, EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs e)
        {
            //tiaojian.ItemsSource = e.Result;
            //if (e.Result != null && e.Result.Count > 0)
            //    tiaojian.SelectedIndex = 0;
            //for (int i = 0; i < tiaojian.Items.Count; i++)
            //{
            //    if (((OrgForHRModel)(tiaojian.Items[i])).ID == model.OrgID)
            //    {
            //        tiaojian.SelectedIndex = i;
            //    }
            //}
        }

        void client1_GetPeiXunDetailModelCompleted(object sender, PeiXunService.GetPeiXunDetailModelCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Count > 0)
            {
                foreach(PeiXunDetailModel i in e.Result)
                {
                    if (i.OrgID == OrgID) model = i;
                }
                if(model.ID == 0)
                {
                    model = e.Result[0];
                }
            }
            
            
            oldpiaoshu = model.PiaoShu;
            olddingpiaoshu = model.DingPiaoShu;
            if (IsNew)
            {
                model.DingPiaoShu = 0;
            }
            if(OrgID == model.OrgID)
            {
                if (olddingpiaoshu == 0)
                {
                    yudingzhangshu1.Visibility = System.Windows.Visibility.Collapsed;
                    yudingzhangshu.Visibility = System.Windows.Visibility.Collapsed;
                    yudingzhangshu2.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    yudingzhangshu.Visibility = System.Windows.Visibility.Visible;
                    yudingzhangshu1.Visibility = System.Windows.Visibility.Visible;
                    yudingzhangshu2.Visibility = System.Windows.Visibility.Visible;
                    yijingpiaoshu = olddingpiaoshu;
                    yudingzhangshu.Text = olddingpiaoshu.ToString();
                }
            }
            
            if (IsNew) model.PeiXunID = peixunid;
            this.DataContext = model;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (model.DingPiaoShu + yijingpiaoshu > model.XianZhiPiaoShu)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("预订票数不能多于票数限制" + model.XianZhiPiaoShu+ "张！");
                return;
            }
            
            model.OrgID = OrgID;
            if (model.OrgID == 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个单位！");
                return;
            }
            if (model.DingPiaoShu == 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请填写预定票数！");
                return;
            }
            client1.CanDingPiaoAsync(model, olddingpiaoshu);

        }
        void client1_CanDingPiaoCompleted(object sender, PeiXunService.CanDingPiaoCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                //model.DingPiaoShu = model.DingPiaoShu + yijingpiaoshu;
                client1.CanDingPiao1Async(model.PeiXunID, model.OrgID, model.DingPiaoShu, olddingpiaoshu,model.XianZhiPiaoShu);
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选培训票数不足！余票为" + model.YuPiao + "张");
                return;
            }
        }
        void client1_CanDingPiao1Completed(object sender, PeiXunService.CanDingPiao1CompletedEventArgs e)
        {
            if (e.Result == true)
            {
                if (IsNew)
                    client1.AddPeiXunDetailModelAsync(model);
                else client1.UpdatePeiXunDetailModelAsync(model, olddingpiaoshu);
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("预订票数不能多于票数限制" + model.XianZhiPiaoShu + "张！");
                return;
            }
        }
        void client1_AddPeiXunDetailModelCompleted(object sender, PeiXunService.AddPeiXunDetailModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("订票成功！");
                //result();
                this.DialogResult = true;
            }
        }
        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Match m = Regex.Match(((TextBox)sender).Text, pattern); // 匹配正则表达式
            if (!m.Success) // 输入的不是数字
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                ((TextBox)sender).Text = "";
                return;
            }
        }
    }
}
